package net.tv90.bfmccore.game.commander;

import net.tv90.bfmccore.game.GameBase;
import net.tv90.bfmccore.game.GameManager;
import org.bitterorange.flansmodslain.FlansModAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;

public class TDMCommanderSystem extends AbstractCommanderSystem {

    public TDMCommanderSystem() {
        super();
    }

    @Override
    public void tick(GameBase gameBase) {
        super.tick(gameBase);
        if (isFirstFactionAutoCommander()) {
            autoMarkHVT(true);
            autoScan(true);
        }
        if (isSecondFactionAutoCommander()) {
            autoMarkHVT(false);
            autoScan(false);
        }
    }


    public void autoMarkHVT(boolean first) {
        if (first) {
            if (firstFactionTickTime % getHIGH_VALUE_TARGET_DELAY() == 0) {
                autoPerformFirstFactionHighValueTarget();
            }
            if (isFirstFactionHighValueTargetExisted()) {
                firstFactionHighValueTargetMarkTime++;
                if (firstFactionHighValueTargetMarkTime >= getHIGH_VALUE_TARGET_MARK_TIME()) {
                    autoUnsetFirstFactionHighValueTarget();
                    firstFactionHighValueTargetMarkTime = 0;
                }
            } else {
                firstFactionHighValueTargetMarkTime = 0;
            }
        } else {
            if (secondFactionTickTime % getHIGH_VALUE_TARGET_DELAY() == 0) {
                autoPerformSecondFactionHighValueTarget();
            }
            if (isSecondFactionHighValueTargetExisted()) {
                secondFactionHighValueTargetMarkTime++;
                if (secondFactionHighValueTargetMarkTime >= getHIGH_VALUE_TARGET_MARK_TIME()) {
                    autoUnsetSecondFactionHighValueTarget();
                    secondFactionHighValueTargetMarkTime = 0;
                }
            } else {
                secondFactionHighValueTargetMarkTime = 0;
            }
        }
    }


    public void autoScan(boolean first) {
        if (first) {
            if (firstFactionTickTime % getSCAN_DELAY() == 0) {
                for (Player p : GameBase.firstFactionList) {
                    FlansModAPI.markEntity(p, 15, p.getWorld().getEntities());
                    p.sendMessage("§e[指挥官]" + GameManager.getInstance().currentGame.secondFactionName + "指挥官"
                            + secondFactionCommander + "已请求15秒全图卫星扫描");
                    noticeScan(p);
                }
                for (Player p : GameBase.secondFactionList) {
                    noticeScanned(p);
                }
            }
        } else {
            if (secondFactionTickTime % getSCAN_DELAY() == 0) {
                for (Player p : GameBase.secondFactionList) {
                    FlansModAPI.markEntity(p, 15, p.getWorld().getEntities());
                    p.sendMessage("§e[指挥官]" + GameManager.getInstance().currentGame.secondFactionName + "指挥官"
                            + secondFactionCommander + "已请求15秒全图卫星扫描");
                    noticeScan(p);
                }
                for (Player p : GameBase.firstFactionList) {
                    noticeScanned(p);
                }
            }
        }
    }

    private void noticeScan(Player p) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "title " + p.getName() + " title 已请求卫星扫描");
    }

    private void noticeScanned(Player p) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "title " + p.getName() + " title 敌方请求卫星扫描");
    }

    //全图扫描间隔，单位秒
    private int getSCAN_DELAY() {
        return 400;
    }

}
